package online.westonline.com.westonline.base;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.easy.west.westclient.api.login.bean.User;
import com.orhanobut.hawk.Hawk;

import org.greenrobot.eventbus.EventBus;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.jpush.im.android.api.JMessageClient;
import online.westonline.com.westonline.utils.CacheUtils;

/**
 * 所有fragment的基类
 * Created by admin on 2018/5/2.
 */

public abstract class BaseFragment extends LibFragment
{
    private Unbinder unbinder;
    private boolean isopen;
    protected ViewGroup container;
    protected AppCompatActivity activity;
    protected User user;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activity = (AppCompatActivity) getActivity();
        isopen = setEventOpen();
        user = CacheUtils.getInstance().getUser();
        initdata();
    }


    protected abstract boolean setEventOpen();


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(setlayout(),container,false);
        this.container = container;
        unbinder = ButterKnife.bind(this,v);
        initview();
        return v;
    }

    protected abstract void initdata();
    protected abstract void initview();
    //返回布局
    protected abstract int setlayout();

    @Override
    public void onStart() {
        super.onStart();
        if (isopen)
        {
            if (!EventBus.getDefault().isRegistered(this))
            {
                EventBus.getDefault().register(this);
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this))
        {
            EventBus.getDefault().unregister(this);
        }
    }

}
